package exam.localdata;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ScheduledThreadPoolExecutor;

/**
 * @author Xiongx
 * @version 1.0
 * @date 2021/3/31 19:02
 * @since JDK 1.8
 *
 * Map<String,Object> objectMaps数据处理
 */
public class MapServer {

    private static Map<String, Object> objectMap=null;

    private static final String name="object";

    public static void main(String[] args) {
       Object obj=getObjectBykey("key1");
    }

    /**
     * 创建数据
     * @param
     */
    public static void addMap(String key,Object value){
        checkMap();
        objectMap.put(key,value);
        System.out.println(objectMap.get(key));;
        wirteObject();
    }

    public static void checkMap(){
        if(objectMap==null){
            Object obj=LocalDataServer.getData(name);
            if(obj==null){
                objectMap=new HashMap<>();
                wirteObject();
            }else {
                objectMap=(Map<String, Object>) obj;
                System.out.println("object赋值了");
            }
        }
    }

    public static Object getObjectBykey(String key){
        checkMap();
        if(!objectMap.containsKey(key)){
            System.out.println("不包含这个key");
            return null;
        }else {
            return objectMap.get(key);
        }
    }

    /**
     * 写入数据
     */
    public static void wirteObject(){
        LocalDataServer.storageData(name,objectMap);
    }


}
